package Gui2;

import algoGenetico.ControleAG;
import algoGenetico.Cromossomo;
import algoGenetico.Individuo;
import algoGenetico.Populacao;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 *
 * @author  Nathy
 */
public class Interface extends javax.swing.JFrame {

    private MainPanel mainPanel;

    public Interface() {

        initComponents();

        this.setTitle("Programa de Rotas");
        mainPanel = new MainPanel() {

            void mouseClicado() {
                if (!grafosButton.isEnabled()) {
                    grafosButton.setEnabled(true);
                }
                if (!reiniciarGrafo.isEnabled()) {
                    reiniciarGrafo.setEnabled(true);
                }
                if (!limpaTudo.isEnabled()) {
                    limpaTudo.setEnabled(true);
                }

            }
        };
        jPanel1.add(mainPanel);
        this.setLocation(20, 0);
        pack();

        this.taxaCruzamento.setText("0.5");
        this.taxaMutacao.setText("0.5");
        this.tamPopulacao.setText("150");
        this.roleta.setSelected(true);
        origem.removeAllItems();
        origem.addItem("Origem");
        destino.removeAllItems();
        destino.addItem("Destino");

        distCalculada.setText("0.0");
        distCalculada.setEditable(false);


    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        origem = new javax.swing.JComboBox();
        destino = new javax.swing.JComboBox();
        grafosButton = new javax.swing.JButton();
        reiniciarGrafo = new javax.swing.JButton();
        limpaTudo = new javax.swing.JButton();
        bestPath = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        taxaCruzamento = new java.awt.TextField();
        taxaMutacao = new java.awt.TextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        roleta = new javax.swing.JCheckBox();
        distCalculada = new java.awt.TextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tamPopulacao = new java.awt.TextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("..:: NXT Ecomp ::..");
        setResizable(false);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(new java.awt.BorderLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setMinimumSize(new java.awt.Dimension(365, 23));
        jToolBar1.setPreferredSize(new java.awt.Dimension(13, 30));

        origem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        origem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                origemMouseClicked(evt);
            }
        });
        origem.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                origemItemStateChanged(evt);
            }
        });
        jToolBar1.add(origem);

        destino.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        destino.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                destinoMouseClicked(evt);
            }
        });
        destino.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                destinoItemStateChanged(evt);
            }
        });
        jToolBar1.add(destino);

        grafosButton.setText("Gerar Grafos");
        grafosButton.setEnabled(false);
        grafosButton.setFocusable(false);
        grafosButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        grafosButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        grafosButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                grafosButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(grafosButton);

        reiniciarGrafo.setText("Limpar Grafo");
        reiniciarGrafo.setEnabled(false);
        reiniciarGrafo.setFocusable(false);
        reiniciarGrafo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        reiniciarGrafo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        reiniciarGrafo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reiniciarGrafoActionPerformed(evt);
            }
        });
        jToolBar1.add(reiniciarGrafo);

        limpaTudo.setText("Limpa Tudo");
        limpaTudo.setEnabled(false);
        limpaTudo.setFocusable(false);
        limpaTudo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        limpaTudo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        limpaTudo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limpaTudoActionPerformed(evt);
            }
        });
        jToolBar1.add(limpaTudo);

        bestPath.setText("Best Path");
        bestPath.setEnabled(false);
        bestPath.setFocusable(false);
        bestPath.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bestPath.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bestPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bestPathActionPerformed(evt);
            }
        });
        jToolBar1.add(bestPath);

        jPanel3.add(jToolBar1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.NORTH);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setPreferredSize(new java.awt.Dimension(100, 25));

        taxaCruzamento.setText("textField1");

        taxaMutacao.setText("textField2");

        jLabel1.setText("Taxa Cruzamento");

        jLabel2.setText("Taxa mutação");

        roleta.setText("Roleta");
        roleta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                roletaActionPerformed(evt);
            }
        });

        distCalculada.setText("textField1");

        jLabel3.setText("Distancia calculda");

        jLabel4.setText("Tamanho População");

        tamPopulacao.setText("textField1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(taxaCruzamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(1, 1, 1)
                .addComponent(taxaMutacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tamPopulacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(roleta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(distCalculada, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(taxaCruzamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(roleta)
                        .addComponent(jLabel4)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(taxaMutacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tamPopulacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(distCalculada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void grafosButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_grafosButtonActionPerformed
        mainPanel.gerarGrafos();
        x = new ControleAG(mainPanel.getMapa(), mainPanel.getOrigem(), mainPanel.getDestino());
        bestPath.setEnabled(true);
    }//GEN-LAST:event_grafosButtonActionPerformed

    private void reiniciarGrafoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reiniciarGrafoActionPerformed
        // TODO add your handling code here:
        mainPanel.reiniciarGrafo();
    }//GEN-LAST:event_reiniciarGrafoActionPerformed

    private void bestPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bestPathActionPerformed
        // TODO add your handling code here:
        Individuo ind =  x.getMelhorIndividuo(Double.valueOf(this.taxaCruzamento.getText()),
                Double.valueOf(this.taxaMutacao.getText()), Integer.valueOf(this.tamPopulacao.getText()), this.roleta.isSelected());
        Cromossomo cromossomo =ind.getCromossomo();
        ArrayList<Point2D> melhorCaminho = new ArrayList<Point2D>();
        for(int cont=0;cont< cromossomo.getTam(); cont++){
            melhorCaminho.add(mainPanel.getMapa().get(cromossomo.getGene(cont)));
        }
        mainPanel.defineBestPath(melhorCaminho);

       distCalculada.setText(String.valueOf(ind.getFitness()));
    }//GEN-LAST:event_bestPathActionPerformed

    private void origemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_origemMouseClicked
        // TODO add your handling code here:
        int numCidades = mainPanel.getNumCidades();

        if (origem.getSelectedIndex() == 0) {
            for (int cont = 0; cont < numCidades; cont++) {

                if (origem.getItemAt(cont + 1) == null) {
                    origem.insertItemAt(cont, cont + 1);
                }
            }
        } else {
            mainPanel.setOrigem(origem.getSelectedIndex() - 1);
        }
    }//GEN-LAST:event_origemMouseClicked

    private void destinoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_destinoMouseClicked
        // TODO add your handling code here:
        int numCidades = mainPanel.getNumCidades();
        if (destino.getSelectedIndex() == 0) {
            for (int cont = 0; cont < numCidades; cont++) {

                if (destino.getItemAt(cont + 1) == null) {
                    destino.insertItemAt(cont, cont + 1);
                }
            }
        } else {
            mainPanel.setDestino(destino.getSelectedIndex() - 1);
        }

    }//GEN-LAST:event_destinoMouseClicked

    private void limpaTudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limpaTudoActionPerformed
        // TODO add your handling code here:
        mainPanel.reiniciarMapa();
        origem.removeAllItems();
        origem.addItem("Origem");
        destino.removeAllItems();
        destino.addItem("Destino");
    }//GEN-LAST:event_limpaTudoActionPerformed

    private void origemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_origemItemStateChanged
        // TODO add your handling code here:
        int aux = origem.getSelectedIndex();
        if(aux >-1){
            mainPanel.setOrigem(aux-1);
        }
    }//GEN-LAST:event_origemItemStateChanged

    private void destinoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_destinoItemStateChanged
        // TODO add your handling code here:
        int aux1 = destino.getSelectedIndex();
        if(aux1 >-1){
            mainPanel.setDestino(aux1-1);
        }
    }//GEN-LAST:event_destinoItemStateChanged

    private void roletaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_roletaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_roletaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bestPath;
    private javax.swing.JComboBox destino;
    private java.awt.TextField distCalculada;
    private javax.swing.JButton grafosButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton limpaTudo;
    private javax.swing.JComboBox origem;
    private javax.swing.JButton reiniciarGrafo;
    private javax.swing.JCheckBox roleta;
    private java.awt.TextField tamPopulacao;
    private java.awt.TextField taxaCruzamento;
    private java.awt.TextField taxaMutacao;
    // End of variables declaration//GEN-END:variables

    ControleAG x;
}
